import 'package:data_plugin/bmob/bmob.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/page/summary_page.dart';
import 'package:flutter_elf/provider/global_provider.dart';
import 'package:flutter_elf/style/system_cupertino_localizations.dart';
import 'package:flutter_elf/utils/my_route_observer.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'base/base_page_state.dart';
import 'generated/i18n.dart';
import 'localstorage/local_storage.dart';

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
const SystemUiOverlayStyle dark = SystemUiOverlayStyle(
  systemNavigationBarColor: Colors.black,
  systemNavigationBarIconBrightness: Brightness.light,
  //在系统导航栏和应用程序内容之间显示指定细线的颜色
  systemNavigationBarDividerColor: null,
  statusBarColor: Colors.transparent,
  statusBarIconBrightness: Brightness.dark,
  statusBarBrightness: Brightness.light,
);
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  LocalStorage.init();
  Bmob.init("https://api2.bmob.cn", "d6120f0280df61012ca8e02b96e3d3b1", "27fd92eaacf8881933b09af7a4d8dc30");
  SystemChrome.setSystemUIOverlayStyle(dark);
//  ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails){
//    globalPrint(flutterErrorDetails.toString());
//    return Center(
//      child: Text("Flutter 走神了"),
//    );
//  };
  //强制竖屏
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
    runApp(MyApp());
    PaintingBinding.instance.imageCache.maximumSize = 100;
  });
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: providers,
      child: Consumer<GlobalProvider>(
        builder: (context,provider,_){
          return RefreshConfiguration(
            autoLoad: true,
            hideFooterWhenNotFull: true,
            //列表数据不满一页,不触发加载更多
            enableBallisticLoad: true,
            // 可以通过惯性滑动触发加载
            footerTriggerDistance: 15,
            dragSpeedRatio: 0.91,
            headerBuilder: () => ClassicHeader(),
            footerBuilder: () => ClassicFooter(loadStyle: LoadStyle.ShowWhenLoading),
            child: MaterialApp(
              navigatorObservers: [MyRouteObserver(), myRouteObserver, /*routeObserver2*/] ,// 导航监听
              debugShowCheckedModeBanner: false,
              localizationsDelegates: const [
                S.delegate,
                ChineseCupertinoLocalizations.delegate,
              ],
              locale: Locale("zh", "en_zh"),
              title: 'Flutter Elf',
              theme: ThemeData(
                primaryColor: provider.getPrimaryColor(),
              ),
              home: SummaryPage(),
            ),
          );
        },
//        child: ,
      ),
    );


//    return ChangeNotifierProvider<GlobalProvider>(
//      create: (context)=>GlobalProvider.instance,
//      child: Consumer<GlobalProvider>(
//        builder: (context,provider,_){
//          return MaterialApp(
//            navigatorObservers: [routeObserver],
//            debugShowCheckedModeBanner: false,
//            localizationsDelegates: const [
//              S.delegate,
//              ChineseCupertinoLocalizations.delegate,
//            ],
//            locale: Locale("zh", "en_zh"),
//            title: 'Flutter Elf',
//            theme: ThemeData(
//              primaryColor: provider.getPrimaryColor(),
//            ),
//            home: SummaryPage(),
//          );
//        },
////        child: ,
//      )
//    );
  }
}
//class HomePage extends BaseWidget{
//  @override
//  BaseWidgetState<BaseWidget> getState() {
//    // TODO: implement getState
//    return HomePageState();
//  }
//}
//
//class HomePageState extends BaseWidgetState{
//  String url;
//  @override
//  Widget buildView(BuildContext context) {
//    // TODO: implement buildView
//    return Column(
//      children: <Widget>[
//        TextField(
//          style: TextStyle(color: Colors.white),
//          onChanged: (String url){
//            this.url = url;
//          },
//        ),
//
//        GestureDetector(
//          onTap: (){
//            NavigatorUtils.navigatorRouter(context, TestPage());
//          },
//          child: Text('4k图片'),
//        ),
//
//
//
//        GestureDetector(
//            onTap: ()=>NavigatorUtils.navigatorRouter(context, JsonWidget()),
////        onTap: ()=>NavigatorUtils.navigatorRouter(context, WebViewPage('https://v.qq.com/x/cover/dh1fyeb58mn9mqt.html')),
//            child: Center(child: Text('JsonWidget',style: TextStyle(fontSize: 20,color: Colors.black),))),
//
//
//        GestureDetector(
//            onTap: ()=>NavigatorUtils.navigatorRouter(context, WebViewTeState(url:this.url)),
////        onTap: ()=>NavigatorUtils.navigatorRouter(context, WebViewPage('https://v.qq.com/x/cover/dh1fyeb58mn9mqt.html')),
//            child: Center(child: Text('内容',style: TextStyle(fontSize: 20,color: Colors.white),))),
//      ],
//    );
//  }
//
//  @override
//  void onCreate() {
//    // TODO: implement onCreate
//    super.onCreate();
//    logs('onCreate');
//  }
//
//  @override
//  void onResume() {
//    // TODO: implement onResume
//    super.onResume();
//    logs('onResume');
//  }
//
//  @override
//  void onDestroy() {
//    // TODO: implement onDestroy
//    super.onDestroy();
//    logs('onDestroy');
//  }
//
//  @override
//  PreferredSizeWidget initAppbar() {
//    // TODO: implement initAppbar
//    return AppBar(
//      title: Text('first'),
//    );
//  }
//}
//
//class SecondPage extends BaseWidget{
//  @override
//  BaseWidgetState<BaseWidget> getState() {
//    // TODO: implement getState
//    return SecondPageState();
//  }
//
//}
//
//class SecondPageState extends BaseWidgetState{
//  @override
//  Widget buildView(BuildContext context) {
//    // TODO: implement buildView
//    return null;
//  }
//  @override
//  void onCreate() {
//    // TODO: implement onCreate
//    super.onCreate();
//    logs('onCreate');
//  }
//
//  @override
//  void onResume() {
//    // TODO: implement onResume
//    super.onResume();
//    logs('onResume');
//  }
//
//  @override
//  void onDestroy() {
//    // TODO: implement onDestroy
//    super.onDestroy();
//    logs('onDestroy');
//  }
//  @override
//  PreferredSizeWidget initAppbar() {
//    // TODO: implement initAppbar
//    return AppBar(
//      title: Text('second'),
//    );
//  }
//
//}